package com.hmall.controller.back;


import com.hdmall.business.common.annotation.AuthRequired;
import com.hdmall.business.common.common.ServerResponse;
import com.hmall.mapper.MallPushMessageMapper;
import com.hmall.model.MallPushMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/manage/message")
@Api(value = "消息管理操作API", description = "消息管理类API说明")
public class MessageManageController {
    private final
    MallPushMessageMapper mallPushMessageMapper;

    @Autowired
    public MessageManageController(MallPushMessageMapper mallPushMessageMapper) {
        this.mallPushMessageMapper = mallPushMessageMapper;
    }

    @AuthRequired
    @GetMapping("getMyMessage")
    @ApiOperation(value = "获得我的消息")
    public ServerResponse getMyMessage(String username, String type, String is_read) {
        List<MallPushMessage> mallPushMessages = mallPushMessageMapper.selectMyMessage(username, type, is_read);
        return ServerResponse.createBySuccess(mallPushMessages);
    }

    @AuthRequired
    @PostMapping("updateMessageStatus")
    @ApiOperation(value = "修改消息状态为已读")
    public ServerResponse updateMessageStatus(Integer messageId) {
        MallPushMessage mallPushMessage = mallPushMessageMapper.selectByPrimaryKey(messageId);
        mallPushMessage.setIsRead(1);
        mallPushMessage.setUpdateTime(new Date());
        mallPushMessageMapper.updateByPrimaryKey(mallPushMessage);
        return ServerResponse.createBySuccess("修改成功");
    }

    @AuthRequired
    @GetMapping("getMessageCount")
    @ApiOperation(value = "获得未读消息数")
    public ServerResponse getMessageCount(Integer user_id) {
        Integer count = mallPushMessageMapper.selectMyMessageCount(user_id);
        return ServerResponse.createBySuccess(count);
    }

}
